/**
 * Project: TacticaPescaditos
 * File: GoalKeeper.java
 */
package org.javahispano.javacup.tacticas_aceptadas.Pescaditos.com.chidotronics.javacup;

import org.javahispano.javacup.modelo.Constantes;
import org.javahispano.javacup.modelo.JugadorDetalle;

import org.javahispano.javacup.tacticas_aceptadas.Pescaditos.com.chidotronics.javacup.fsm.StateMachine;
import org.javahispano.javacup.tacticas_aceptadas.Pescaditos.com.chidotronics.javacup.fsm.state.GlobalKeeperState;
import org.javahispano.javacup.tacticas_aceptadas.Pescaditos.com.chidotronics.javacup.fsm.state.TendGoal;
import org.javahispano.javacup.tacticas_aceptadas.Pescaditos.com.chidotronics.javacup.geometrics.Vector2D;
import org.javahispano.javacup.tacticas_aceptadas.Pescaditos.com.chidotronics.javacup.messages.Telegram;

/**
 * @author Carlos Alberto
 * class: GoalKeeper
 */
public class GoalKeeper extends Player<GoalKeeper> {
	
	public static final double TENDING_DISTANCE = Constantes.LARGO_ARCO/2+0.1d;
	
	public static final double MAX_DISTANCE_SQ = Constantes.ANCHO_AREA_CHICA*Constantes.ANCHO_AREA_CHICA/4 + Constantes.LARGO_AREA_CHICA*Constantes.LARGO_AREA_CHICA/4 +1;

	private static final double INTERCEPT_RANGE_SQ = 
		Constantes.ANCHO_AREA_GRANDE*Constantes.ANCHO_AREA_GRANDE/4 + Constantes.LARGO_AREA_GRANDE*Constantes.LARGO_AREA_GRANDE/4;


	


	private StateMachine<GoalKeeper> stateMachine;

	public GoalKeeper(TacticaPescaditos tacticaPescaditos, JugadorDetalle detail) {
		super(tacticaPescaditos, detail, PLAYER_ROLE.GOAL_KEEPER, Constantes.ALTO_ARCO);


		this.setHomePosition(new Vector2D(0,-52));
		stateMachine = new StateMachine<GoalKeeper>(this);
		stateMachine.setGlobalState(GlobalKeeperState.instance);
		
		stateMachine.setCurrentState( TendGoal.instance);
		stateMachine.getCurrentState().enter(this);
	}




	public Vector2D getRearInterposeTarget() {
		/*
		  double xPosTarget = (getBall().getPosition().x*Constantes.LARGO_ARCO) /
		  							Constantes.ANCHO_CAMPO_JUEGO;	  

		 

		  return new Vector2D(xPosTarget, getHomePosition().getY()); 
		  */
		Vector2D inter = getBall().getTrajectory().intersection(owner.getGoalSegment());
		if (inter == null || Math.abs(inter.getX()) >= Constantes.LARGO_ARCO/2){
			inter = getHomePosition();
		} 
		
		return inter;

	}




	@Override
	public void handleMessage(Telegram telegram) {
		
	}




	@Override
	public void resetStateMachine() {
		stateMachine.changeState( TendGoal.instance);
		
	}








	@Override
	public StateMachine<GoalKeeper> getStateMachine() {
		return stateMachine;
	}




	public boolean isFarFromGoal() {
		  return getPosition().distanceSq(this.getHomePosition())>INTERCEPT_RANGE_SQ;
	}




	public boolean ballWithinRangeForIntercept() {
		
		return getBall().getPosition().distanceSq(getHomePosition())<INTERCEPT_RANGE_SQ;
	}





}
